package com.approval.approvalapp.knowledge;


import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.knowledge.wj.WjFragment;
import com.approval.approvalapp.knowledge.zcfg.ZcfgFragment;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * A simple {@link Fragment} subclass.
 */
public class KnowladgeFragment extends BaseFragment {

    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.vp)
    ViewPager vp;

    private List<Fragment> fragments;
    private String[] titles;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_knowladge;
    }

    @Override
    public void initView(View view) {
        fragments = new ArrayList<>();
        if (!SpUtil.getString(this.getActivity(),SpUtil.Usertype , "").equals("1")){
            titles = new String[]{ "文件","政策法规"};

            WjFragment wjFragment = new WjFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type","0");
            wjFragment.setArguments(bundle);
            fragments.add(wjFragment);

            fragments.add(new ZcfgFragment());


        }else {
            titles = new String[]{"文件","通用文档" , "共享文档","政策法规" };


            WjFragment wjFragment = new WjFragment();
            WjFragment wjFragment2 = new WjFragment();
            WjFragment wjFragment3 = new WjFragment();


            Bundle bundle1 = new Bundle();
            bundle1.putString("type","0");
            wjFragment.setArguments(bundle1);
            fragments.add(wjFragment);

            Bundle bundle2 = new Bundle();
            bundle2.putString("type","1");
            wjFragment2.setArguments(bundle2);
            fragments.add(wjFragment2);

            Bundle bundle3 = new Bundle();
            bundle3.putString("type","2");
            wjFragment3.setArguments(bundle3);
            fragments.add(wjFragment3);

            fragments.add(new ZcfgFragment());

        }

        vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        tablayout.setupWithViewPager(vp);
        for(int i = 0 ;i<fragments.size();i++){
            //获得第i个位置的标签的视图
            View tabView = getTabView(i,this.getActivity());
            //获得第i位置的Tab 对象
            TabLayout.Tab tab = tablayout.getTabAt(i);
            tab.setCustomView(tabView);
        }

    }

    @Override
    public void initData() {

    }

    public View getTabView(int index, Context context) {
        //通过LayoutInflater类的静态方法 from 得到一个LayoutInflater 对象
        LayoutInflater inflater = LayoutInflater.from(context);
        //通过inflater对象的inflate方法 将布局xml注入成一个view对象
        View tabView = inflater.inflate(R.layout.tabview, null);
        //通过tabView对象,找到视图中的TextView
        TextView titleTv = (TextView) tabView.findViewById(R.id.tv);
        //设置标题
        titleTv.setText(titles[index]);
        return tabView;
    }
}



